﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace KHOALUAN
{
    public partial class frmDangKyMonGiangDay : Form
    {
        clsMain.XMLData MH = new clsMain.XMLData();
        public frmDangKyMonGiangDay()
        {
            InitializeComponent();
        }
        private void LoadMonHoc()
        {
            try
            {
                
                XElement xGV = XElement.Load(clsMain.DATA_PATH + "GIAOVIEN" + ".xml");
                IEnumerable<XElement> colGiaoVien = from Giaovien in xGV.Elements("GIAOVIEN")
                                                    where Giaovien.Element("MSGV").Value.ToString() == clsMain.strGiangVienCode
                
                                                    select Giaovien;
                XElement xelement = XElement.Load(clsMain.DATA_PATH + "MONHOC" + ".xml");
                IEnumerable<XElement> Columns = xelement.Elements();
                System.Data.DataTable dt = new System.Data.DataTable();
                foreach (var col in Columns)
                {
                        if (col.Name == "ColumnNames")
                        {
                            for (int j = 0; j < MH.SoColumn; j++)
                            {
                                dt.Columns.Add(col.Element(MH.Names[j]).Value, typeof(string));
                            }
                        }
                }
                foreach (var col in Columns)
                {
                    bool flag = true;
                    foreach (var colMon in colGiaoVien.Elements("MONGIANGDAY"))
                    {
                        if (colMon.Element("NAME").Value == col.Element("MSMH").Value)
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (col.Name != "ColumnNames" && flag==true)
                    {
                        System.Data.DataRow row = dt.NewRow();
                        for (int j = 0; j < MH.SoColumn; j++)
                        {
                            row[j] = col.Element(MH.Names[j]).Value;
                        }
                        dt.Rows.Add(row);
                    }
                }
                
                // Read the entire XML
                //int j = 0;
                //System.Data.DataTable dt = new System.Data.DataTable();
               /* foreach (var col in Columns)
                {
                    if (col.Name == "ColumnNames")
                    {
                        for (int j = 0; j < MH.SoColumn; j++)
                        {
                            dt.Columns.Add(col.Element(MH.Names[j]).Value, typeof(string));
                        }
                    }
                    else
                    {
                        System.Data.DataRow row = dt.NewRow();
                        for (int j = 0; j < MH.SoColumn; j++)
                        {
                            row[j] = col.Element(MH.Names[j]).Value;
                        }
                        dt.Rows.Add(row);
                    }
                }
                */
                dgvMonHoc.DataSource = dt;
                for (int i = 0; i < MH.SoColumn; i++)
                {
                    dgvMonHoc.Columns[i].Tag = MH.Names[i];

                }




                // clsMain.AddDataToDataGridViewMonHoc("MONHOC", ref dgvMonHoc, MH);
            }
            catch (Exception ex)
            {
                MessageBox.Show("LoadMonHoc... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }

        private void frmDangKyMonGiangDay_Load(object sender, EventArgs e)
        {
            try
            {
                
                lblGiangVien.Text = "Giảng viên: " + clsMain.strGiangVien;
                clsMain.KhoiTaoXMLTable("MONHOC", ref MH, 2);
                MH.ColumnName[0] = "MSMH";
                MH.ColumnName[1] = "MÔN HỌC";
                MH.Names[0] = "MSMH";
                MH.Names[1] = "TENMH";
                LoadMonDangKy();
                LoadMonHoc();
            }
            catch (Exception ex)
            {
                MessageBox.Show("frmDangKyMonGiangDay_Load... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }

        private void LoadMonDangKy()
        {
            try
            {
                // Read the entire XML
                //int j = 0;
                XElement xGV = XElement.Load(clsMain.DATA_PATH + "GIAOVIEN" + ".xml");
                IEnumerable<XElement> colGiaoVien = from Giaovien in xGV.Elements("GIAOVIEN")
                                                    where Giaovien.Element("MSGV").Value.ToString() == clsMain.strGiangVienCode

                                                    select Giaovien;
                XElement xelement = XElement.Load(clsMain.DATA_PATH + "MONHOC" + ".xml");
                IEnumerable<XElement> Columns = xelement.Elements();
                System.Data.DataTable dt = new System.Data.DataTable();
                foreach (var col in Columns)
                {
                    if (col.Name == "ColumnNames")
                    {
                        for (int j = 0; j < MH.SoColumn; j++)
                        {
                            dt.Columns.Add(col.Element(MH.Names[j]).Value, typeof(string));
                        }
                        break;
                    }
                }
                foreach (var col in colGiaoVien.Elements("MONGIANGDAY"))
                 {
                     if (col.Name == "ColumnNames")
                     {
                         for (int j = 0; j < MH.SoColumn; j++)
                         {
                             dt.Columns.Add(col.Element(MH.Names[j]).Value, typeof(string));
                         }
                     }
                     else
                     {
                         System.Data.DataRow row = dt.NewRow();
                         
                             row[0] = col.Element("NAME").Value;
                             row[1] = col.Element("VALUE").Value;

                         dt.Rows.Add(row);
                     }
                 }
                 dgvMonDangKy.DataSource = dt;
                 for (int i = 0; i < MH.SoColumn; i++)
                 {
                     dgvMonDangKy.Columns[i].Tag = MH.Names[i];

                 }
            }
            catch (Exception ex)
            {
                MessageBox.Show("LoadMonDangKy... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }


        private void butAdd_Click(object sender, EventArgs e)
        {
            if (dgvMonHoc.SelectedCells[0].Value != null && dgvMonHoc.SelectedCells[1].Value != null)
            {
                string strElement = dgvMonHoc.SelectedCells[0].Value.ToString();
                
                XElement xEmp = XElement.Load(clsMain.DATA_PATH + "GIAOVIEN" + ".xml");
                //
                var empDetails = from emps in xEmp.Elements("GIAOVIEN")
                                 where emps.Element("MSGV").Value.Equals(clsMain.strGiangVienCode)
                                 select emps;
                foreach (XElement xEle in empDetails)
                {

                    xEle.Add(new XElement("MONGIANGDAY", new XElement("NAME", dgvMonHoc.SelectedCells[0].Value.ToString()), new XElement("VALUE", dgvMonHoc.SelectedCells[1].Value.ToString())));
                    break;
                    

                }
                xEmp.Save(clsMain.DATA_PATH + "GIAOVIEN" + ".xml");
                //dgvMonHoc.Rows.Clear();
                LoadMonHoc();
                LoadMonDangKy();
                //ClearControls(FormID);
            }
        }

        private void butRemove_Click(object sender, EventArgs e)
        {
            try
            {
                
                XElement xGV = XElement.Load(clsMain.DATA_PATH + "GIAOVIEN" + ".xml");
                var colGiaoVien = from Giaovien in xGV.Elements("GIAOVIEN")
                                  where (Giaovien.Element("MSGV").Value == clsMain.strGiangVienCode)
                                  select Giaovien;
                foreach (var col in colGiaoVien.Elements("MONGIANGDAY"))
                {
                    if (col.Element("NAME").Value == dgvMonDangKy.SelectedCells[0].Value.ToString())
                    {
                        col.Remove();
                        break;
                    }
                }
                
                xGV.Save(clsMain.DATA_PATH + "GIAOVIEN" + ".xml");
                LoadMonHoc();
                LoadMonDangKy();
            }
            catch (Exception ex)
            {
                MessageBox.Show("butRemove_Click... \n\nChi tiết:\n" + ex, "Thông báo lỗi");
            }
        }
    }
}
